package projet.ejb;

import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import projet.entity.Participation;
import projet.entity.Startup;

@Stateless
public class ParticipationFacadeEJB implements ParticipationFacade {
	@PersistenceContext(unitName = "SampleUnit")
	protected EntityManager entityMgr;
	
	public Participation add(Participation p) {
		entityMgr.persist(p);
		return p;		
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Participation> findByStartup(Startup startup){
		Query query = entityMgr.createQuery("SELECT p FROM Participation as p WHERE p.startup=:s ");
		query.setParameter("s", startup);
		return (Collection<Participation>) query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Participation> findDisponibleByStartup(Startup startup){
		Query query = entityMgr.createQuery("SELECT p FROM Participation as p WHERE (p.startup=:s and p.disponible=:d )");
		query.setParameter("s", startup);
		query.setParameter("d", 1);
		return (Collection<Participation>) query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Participation> findAllDisponible() {
		Query query = entityMgr.createQuery("SELECT p FROM Participation as p WHERE p.disponible=:d");
		query.setParameter("d", 1);	
		return (Collection<Participation>) query.getResultList();
		}

	@Override
	public Participation update(Participation p) {
		return entityMgr.merge(p);
	}	
}
